package com.rds.material.utils;

import com.rds.common.utils.SecurityUtils;
import com.rds.system.domain.RdaPolicy;
import com.rds.system.domain.RdaState;
import com.rds.system.domain.RdaType;
import com.rds.system.service.IRdaPolicyService;
import com.rds.system.service.IRdaStateService;
import com.rds.system.service.IRdaTypeService;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WorkspaceUtils {

    /**
     * 获取 类型、策略、状态id或转码结果
     * @param policyName  策略名称
     * @param type 1 返回类型策略状态id 2返回类型策略状态转码后的中文状态
     * @return 获取 类型、策略、状态id或转码结果
     */
    public static Map<String,Object> getTypePolicyState(String policyName, String type, IRdaTypeService rdaTypeService, IRdaPolicyService rdaPolicyService, IRdaStateService rdaStateService){
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map2=new HashMap<String,Object>();

        RdaPolicy rdaPolicy=new RdaPolicy();
        rdaPolicy.setName(policyName);
        List<RdaPolicy> policyList=rdaPolicyService.selectRdaPolicyList(rdaPolicy);
        if(policyList!=null && policyList.size()!=0) {
            for(RdaPolicy policy:policyList){
                if(policy.getName().equals(policyName)){
                    rdaPolicy=policy;
                }
            }
            RdaState rdaState = new RdaState();
            rdaState.setPolicyid(rdaPolicy.getId());
            Long Typeid = Long.valueOf(rdaPolicy.getTypeIds());//类型id
            Long Policyid = rdaPolicy.getId();//策略id
            List<RdaState> stateList = rdaStateService.selectRdaStateList(rdaState);
            if ("1".equals(type)) {
                for (RdaState state : stateList) {
                    Long stateid = state.getId();
                    map2.put(state.getName(), stateid);
                }
                map.put("Typeid", Typeid);
                map.put("Policyid", Policyid);
                map.put("Stateid", map2);
            } else if ("2".equals(type)) {
                for (RdaState state : stateList) {
                    map2.put(state.getName(), state.getDescription());
                }
                RdaType rdaType=rdaTypeService.selectRdaTypeById(Typeid);
                map.put("TypeName",rdaType.getDescription());
                map.put("PolicyName",rdaPolicy.getDescription());
                map.put("StateName",map2);
            } else {
                return null;
            }
        }else {
            return null;
        }
        return map;
    }

    /**
     * 文件大小转换
     * @param size 原文件大小
     * @return 转换后的文件大小
     */
    public static String readableFileSize(long size) {
        if (size <= 0) return "0";
        final String[] units = new String[]{"B","KB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

    /**
     * 获取文件后缀
     * @param fileName 文件名
     * @param type 文件名 1返回带.txt 2返回 txt
     * @return 转换后的文件大小
     */
    public static String getFileSuffix(String fileName,String type){
        String suffix="";
        if("1".equals(type)){
            suffix = fileName.substring(fileName.lastIndexOf(".") + 0);
        }else if("2".equals(type)){
            suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        return suffix;
    }




    /**
     * 获取 类型、策略、状态id或转码结果
     * @param policyName  策略名称
     * @param type 1 返回类型策略状态id 2返回类型策略状态转码后的中文状态
     * @return 获取 类型、策略、状态id或转码结果
     */
    public static Map<String,Object> getTypePolicyStates(String policyName, String type, IRdaTypeService rdaTypeService, IRdaPolicyService rdaPolicyService, IRdaStateService rdaStateService){
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map2=new HashMap<String,Object>();
        Map<String,Long> type2=new HashMap<String,Long>();
        Map<String,String> type3=new HashMap<String,String>();
        RdaPolicy rdaPolicy=new RdaPolicy();
        rdaPolicy.setName(policyName);
        List<RdaPolicy> policyList=rdaPolicyService.selectRdaPolicyList(rdaPolicy);
        if(policyList!=null && policyList.size()!=0) {
            for(RdaPolicy policy:policyList){
                if(policy.getName().equals(policyName)){
                    rdaPolicy=policy;
                }
            }
            RdaState rdaState = new RdaState();
            rdaState.setPolicyid(rdaPolicy.getId());
            if(rdaPolicy.getTypeIds().contains(",")){
                String typeIds[] = rdaPolicy.getTypeIds().split(",");
                for(String typeId:typeIds){
                    RdaType rdaType= rdaTypeService.selectRdaTypeById(Long.valueOf(typeId));
                    type2.put(rdaType.getName(),rdaType.getId());
                    type3.put(rdaType.getName(),rdaType.getDescription());
                }
            }else{
                Long Typeid = Long.valueOf(rdaPolicy.getTypeIds());//类型id
                RdaType rdaType= rdaTypeService.selectRdaTypeById(Typeid);
                type2.put(rdaType.getName(),rdaType.getId());
                type3.put(rdaType.getName(),rdaType.getDescription());
            }
            Long Policyid = rdaPolicy.getId();//策略id
            List<RdaState> stateList = rdaStateService.selectRdaStateList(rdaState);
            if ("1".equals(type)) {
                for (RdaState state : stateList) {
                    Long stateid = state.getId();
                    map2.put(state.getName(), stateid);
                }
                map.put("Typeid", type2);
                map.put("Policyid", Policyid);
                map.put("Stateid", map2);
                map.put("revision",rdaPolicy.getRevision());
                map.put("revisionRule",rdaPolicy.getRevisionRule());
            } else if ("2".equals(type)) {
                for (RdaState state : stateList) {
                    map2.put(state.getName(), state.getDescription());
                }
                map.put("TypeName",type3);
                map.put("PolicyName",rdaPolicy.getDescription());
                map.put("StateName",map2);
                map.put("revision",rdaPolicy.getRevision());
                map.put("revisionRule",rdaPolicy.getRevisionRule());
            } else {
                return null;
            }
        }else {
            return null;
        }
        return map;
    }
}
